﻿using System;
using Sunny.UI;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ClassLibrary;
using MainForm;

namespace Forms
{
    public partial class PayConfirm : UIForm
    {
        User user = new User();
        Order order = new Order();
        List<Good> goods = new List<Good>();
        Supermarket supermarket = new Supermarket();
        OrderService orderService = new OrderService();
        GoodService goodService = new GoodService();
        ItemService itemService = new ItemService();
        public PayConfirm(User user, Order order, List<Good> goods, Supermarket supermarket)
        {
            InitializeComponent();
            this.user = user;
            this.order = order;
            this.goods = goods;
            this.supermarket = supermarket;
            panel.AutoScroll = true; // 启用自动滚动条
            InitPaybox();//初始化窗口内容
            Color userColor = MainForm.MainForm.userColor;
            UIStyles.InitColorful(userColor, Color.White);
        }
        public void InitPaybox()
        {
            //初始化个人消息
            NameLabel.Text = "姓名：" + user.UserName;
            Tele.Text = "电话：" + user.UserTele;
            UserID.Text = "学号：" + user.UserID;
            Address.Text = "收货地址：" + user.UserAddress;
            //初始化订单详情
            int i = 1;
            foreach (Good good in goods)
            {
                Output.Text += "商品" + i + "：\n";
                Output.Text += "名称：" + good.GoodName + "\n数量：" + good.GoodCount + "\n单价：" + good.GoodPrice + "¥\n\n";
                i++;
            }
            SumLabel.Text = "总金额：" + order.Sum + "¥";
        }

        private void BuyBtn_Click(object sender, EventArgs e)
        {
            //添加订单
            orderService.AddOrder(order);
            //修改Item的剩余数量，添加每一件购买的商品
            foreach (Good good in goods)
            {
                good.OrderID = order.OrderID;//向good存入订单ID
                goodService.AddGood(good);//添加good
                //查询每个购买过的商品
                List<Item> item = itemService.GetItemByItemID(good.ItemID);
                Item olditem = item[0];
                Item newitem = item[0];//更新newitem的剩余数量
                newitem.ItemCount -= good.GoodCount;
                itemService.ModifyItem(olditem, newitem);//修改Item的剩余数量
            }
            //更新商品列表和总金额
            List<Item> items = itemService.GetItemExceptUser(user);
            supermarket.ListItems(items);
            supermarket.SumLabel.Text = "0.00";
            //清空goods表，以进行下一笔订单的操作
            supermarket.goods.Clear();
            //提示
            UIMessageBox.Show("订单已提交");
            this.Close();
        }
    }
}
